package com.yq.interview.jvm.study;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

/**
 * @ClassName ReferenceQueueDemo
 * @Description TODO
 * @Author yaoqiang
 * @Date 2020/11/25 13:08
 * @Version 1.0
 */
public class ReferenceQueueDemo {
    public static void main(String[] args) throws InterruptedException {
/*        Object obj1=new Object();
        ReferenceQueue<Object> referenceQueue=new ReferenceQueue<>();
        WeakReference<Object> weakReference=new WeakReference<>(obj1,referenceQueue);
        System.out.println(obj1);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
        System.out.println("=======");
        obj1=null;
        System.gc();
            Thread.sleep(500);
        System.out.println(obj1);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());*/
//虚引用
        Object object=new Object();
        ReferenceQueue<Object> referenceQueue=new ReferenceQueue<>();
        PhantomReference<Object> phantomReference=new PhantomReference<>(object,referenceQueue);
        System.out.println(object);
        System.out.println(phantomReference.get());
        System.out.println(referenceQueue.poll());
        object=null;
        System.gc();
        Thread.sleep(500);
        System.out.println(object);
        System.out.println(phantomReference.get());
        System.out.println(referenceQueue.poll());//类似AOP的后置通知
    }
}
